# import multiprocessing
# import time
# import os
# # 如何用?
# # 用多进程干什么?——-同时干多个事情。
# # 事情1
# def learn(km):
#     while True:
#         print(f"{time.strftime('%Y-%m-%d %H:%M:%S')}:我正在学习{km}。我的进程号是{os.getpid()}")
#         time.sleep(2)
# # 事情2
# def listen(name,singger="刘德华"):
#     while True:
#         print(f"{time.strftime('%Y-%m-%d %H:%M:%S')}:我正在听{singger}的{name}。我的进程号是{os.getpid()}")
#         time.sleep(2)
#
# if __name__ == '__main__':
#     # 打印进程号
#     print("主进程号是:",os.getpid())
#     p1=multiprocessing.Process(target=learn,name="learn",args=("语文",),daemon=True)  # 准备进程
#     # p2=multiprocessing.Process(target=listen,name="listen" ,args=("忘情水",),kwargs={"singger":"刘德华"}  # 准备进程
#     p2=multiprocessing.Process(target=listen,name="listen",kwargs={"name":"忘情水","singger":"刘德华"},daemon=True)  # 准备进程
#     p1.start()
#     p2.start()
#     time.sleep(2)
#     print('主进程运行到这里了')


# import time,os
# from multiprocessing import Process
#
# def A(n):
#     while True:
#         print(f'{os.getpid()}:我要吃{n}')
#         time.sleep(1)
#
# if __name__ == '__main__':
#     print('开始时间：',time.strftime('%Y-%m-%d %H:%M:%S'))
#
#     p1=Process(target=A,name="进程1",args=('苹果',),daemon=True)
#     p2=Process(target=A,name="进程2",args=('香蕉',),)
#     p2.daemon = True
#     p1.start()
#     p1.join(2)
#     p1.terminate()
#     p2.start()
#     time.sleep(3)

import time,random
from multiprocessing import Queue,Process  # 队列

def tree(q):
    no = 1
    while True:
        apple = f'苹果{no}'
        q.put(apple)
        print(f"{time.strftime('%H:%M:%S')}：添加一个{apple}，共有{q.qsize()}个苹果.")
        no += 1
        time.sleep(random.randint(1,2))

def eat(q,name):
    while True:
        print(f'{name}吃掉了{q.get()},剩余：{q.qsize()}')
        time.sleep(random.randint(2,3))

if __name__ == '__main__':
    q = Queue(maxsize=6)
    t=Process(target=tree,args=(q,))
    p1 = Process(target=eat, args=(q, '张三'))
    p2 = Process(target=eat, args=(q, '李四'))
    t.start()
    t.join(10)
    p1.start()
    p2.start()





# print("判断：",q.full())
#
# a1 = q.get()
# a2 = q.get()
# a3 = q.get()
# print("是否为空：",q.empty())
#
# print(q.qsize())










